import javax.swing.RowFilter.ComparisonType;


public class CTable 
{	
	public enum enum_SituationOnTheTable
	{
		SOTT_NULL, SOTT_CARDS_DEALT, SOTT_CARDS_CHANGED, SOTT_CARDS_CHECKED;
	}
	
	public enum enum_ResultOfAction
	{
		ROA_PLAYER_NOT_EXIST, ROA_NEGATIVE_INDEX, ROA_REPEATED_INDEX, ROA_DONE;
	}
	
	private CDeck deck_TableDeck;
	private CPlayer[] play_ArrayOfPlayers;
	private int int_HowManyPlayers;
	private enum_SituationOnTheTable sott_WhatNow;
	 
	CTable(CPlayer play_First, CPlayer play_Second)
	{
		this(play_First, play_Second, null ,null, null);
	}
	
	CTable(CPlayer play_First, CPlayer play_Second, CPlayer play_Third)
	{
		this(play_First, play_Second, play_Third ,null, null);
	}
	
	CTable(CPlayer play_First, CPlayer play_Second, CPlayer play_Third, CPlayer play_Fourth)
	{
		this(play_First, play_Second, play_Third ,play_Fourth, null);
	}
	
	CTable(CPlayer play_First, CPlayer play_Second, CPlayer play_Third, CPlayer play_Fourth, CPlayer play_Fifth)
	{
		CPlayer[] play_ArrayOfPlayers = new CPlayer[4];
		play_ArrayOfPlayers[0]=play_First;
		play_ArrayOfPlayers[1]=play_Second;
		play_ArrayOfPlayers[2]=play_Third;
		play_ArrayOfPlayers[3]=play_Fourth;
		play_ArrayOfPlayers[4]=play_Fifth;
				
		deck_TableDeck = new CDeck();
		
		for(int i=0;i<5;++i)
		{
			if(play_ArrayOfPlayers[i]==null){break;}
			++int_HowManyPlayers;
		}
		
		if(int_HowManyPlayers<2)
		{
			sott_WhatNow=CTable.enum_SituationOnTheTable.SOTT_NULL;
			return;
		}
		
		for(int i=0;i<int_HowManyPlayers;++i)
		{
			for(int j=0;j<5;++j){play_ArrayOfPlayers[i].deck_PlayerDeck.setCardFromDeck(deck_TableDeck.grabNextCard(),j);}
		}
		sott_WhatNow = CTable.enum_SituationOnTheTable.SOTT_CARDS_DEALT;
	}
	
	public enum_ResultOfAction AllPlayersChangeTheCards(CPlayer play_Who[], int[][] int_ArrayOfArguments)
	{
		for(int int_Counter=0;int_Counter<int_HowManyPlayers;++int_Counter)
		{
			if(play_Who[int_Counter]==null) return enum_ResultOfAction.ROA_PLAYER_NOT_EXIST;
			if(int_ArrayOfArguments[int_Counter].length>=0 && int_ArrayOfArguments[int_Counter].length<5)
			{
				CQuick.quicksort(int_ArrayOfArguments[int_Counter], 0, int_ArrayOfArguments[int_Counter].length);
				
				if(int_ArrayOfArguments[int_Counter][int_ArrayOfArguments[int_Counter].length]<0){return enum_ResultOfAction.ROA_NEGATIVE_INDEX;}
				for(int i=0;i<int_ArrayOfArguments[int_Counter].length;++i){if(int_ArrayOfArguments[int_Counter][i]==int_ArrayOfArguments[int_Counter][i+1]){return enum_ResultOfAction.ROA_REPEATED_INDEX;}}
				
				for(int i=0;i<int_ArrayOfArguments[int_Counter].length;++i){play_Who[int_Counter].deck_PlayerDeck.setCardFromDeck(deck_TableDeck.grabNextCard(), int_ArrayOfArguments[int_Counter][i]);}
			}
		}
		sott_WhatNow = enum_SituationOnTheTable.SOTT_CARDS_CHANGED;
		return enum_ResultOfAction.ROA_DONE;
	}
	
	public CPlayer[] CheckCardsOnTable()
	{
		CPlayer[] play_Winners = new CPlayer[int_HowManyPlayers];
		play_Winners[0] = new CPlayer(false);
		play_Winners[0] = play_ArrayOfPlayers[0];
		int int_Index = 0;
		
		for(int i=0;i<int_HowManyPlayers-1;++i)
		{
			if(CDeck.ComparisonSequence(play_ArrayOfPlayers[i].deck_PlayerDeck.getSequenceOfCards(),
			play_ArrayOfPlayers[i+1].deck_PlayerDeck.getSequenceOfCards())==CDeck.enum_ResultOfComparison.ROC_LARGER)
			{
				for(int j=1;j<int_HowManyPlayers;++j){play_Winners[j]=null;}
				int_Index = 0;
				play_Winners[int_Index]=play_ArrayOfPlayers[i+1];
			}
			else if(CDeck.ComparisonSequence(play_ArrayOfPlayers[i].deck_PlayerDeck.getSequenceOfCards(),
					play_ArrayOfPlayers[i+1].deck_PlayerDeck.getSequenceOfCards())==CDeck.enum_ResultOfComparison.ROC_EQUAL)
			{
				++int_Index;
				play_Winners[int_Index]= new CPlayer(false);
				play_Winners[int_Index]=play_ArrayOfPlayers[i+1];
			}
		}
		sott_WhatNow = enum_SituationOnTheTable.SOTT_CARDS_CHECKED;
		return play_Winners;
	}
}
